package com.car.carcontrol.base;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.view.inputmethod.InputMethodManager;

import com.car.carcontrol.AppManager;
import com.easy.carpanel.uart.uartJni;
import com.easy.carpanel.uart.uartJnit;
import com.example.x6.serial.SerialPort;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;


public class BaseActivity extends BaseFragmentActivity {

	private static final int MSG_SET_ALIAS = 1001;
	private static final int MSG_SET_TAGS = 1002;

	private SerialPort serialttyS1;
	public InputStream ttyS1InputStream;
	public OutputStream ttyS1OutputStream;

	public static int width = BaseApplication.width;
	public static int height = BaseApplication.height;
	public static float density = BaseApplication.density;

	public static int width_d = 246*width/1280;
	public static int height_d = 140*height/480;

	public byte[] read_buf = new byte[1024];
	public static  int fd;

	@Override
	protected void onCreate(Bundle bundle) {
		super.onCreate(bundle);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);
		BaseApplication.context = context;
//		ButterKnife.bind(this);

//		setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);

		AppManager.getAppManager().addActivity(this);
		// 修改状态栏颜色，4.4+生效
//		if (VERSION.SDK_INT >= VERSION_CODES.KITKAT) {
//			// 透明状态栏
//			getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
//			// 透明导航栏
//			getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
//		}

//		init_serial();

		fd =  uartJnit.getInstance().fd;
	}

//	/* 打开串口 */
//	private void init_serial() {
//		try {
//			serialttyS1 = new SerialPort(new File("/dev/ttyS1"),115200,0);
//			ttyS1InputStream = serialttyS1.getInputStream();
//			ttyS1OutputStream = serialttyS1.getOutputStream();
//		} catch (IOException e) {
//			e.printStackTrace();
//		}
//	}
//
//	public void write(byte[] bytes){
//		/* 串口发送字节 */
//		try {
//			ttyS1OutputStream.write(bytes);   //TODO
//		} catch (Exception e) {
//			e.printStackTrace();
//		}
//	}
//
//	public void read(byte[] bytes){
//		/* 串口发送字节 */
//		try {
//			int temp = ttyS1InputStream.read(bytes);   //TODO
////			tv_temp.setText(temp+"℃");
//		} catch (Exception e) {
//			e.printStackTrace();
//		}
//	}



//	/* 打开串口 */
//	private void init_serial() {
//		try {
//			fd = uartJni.open("/dev/ttyAMA2", 115200);
//		} catch (Exception e) {
//			e.printStackTrace();
//		}
//	}
//	/* 串口发送字节 */
//	public void write(byte[] bytes){
//		try {
//			uartJni.write(fd, bytes, bytes.length);
//		} catch (Exception e) {
//			e.printStackTrace();
//		}
//	}
	public static String toHex(byte[] buffer,int read_count) {
		StringBuffer sb = new StringBuffer(read_count * 2);
		for (int i = 0; i < read_count; i++) {
			sb.append(Character.forDigit((buffer[i] & 240) >> 4, 16));
			sb.append(Character.forDigit(buffer[i] & 15, 16));
		}
		return sb.toString();
	}
	public static byte charToByte(char c) {
		byte b = (byte) "0123456789abcdef".indexOf(c);
		return b;
	}
	public static byte[] hexStringToBytes(String hex) {
		while (hex.length() < 2)
			return null;
		int len = (hex.length() / 2);
		byte[] result = new byte[len];
		char[] achar = hex.toCharArray();
		for (int i = 0; i < len; i++) {
			int pos = i * 2;
			result[i] = (byte) (charToByte(achar[pos]) << 4 | charToByte(achar[pos + 1]));
		}
		return result;
	}
//	/* 串口发送字节 */
//	public int read(byte[] bytes){
//		try {
//			read_buf = new byte[1024];
//			int read_count = uartJni.read(fd, read_buf, read_buf.length);
//			if (read_count < 0) {
//				read_count = 0;
//			}
//
////			String tmpstr = toHex(read_buf, read_count).toString();
////			if (!tmpstr.equals("")) {
////				int index;
////				index = tmpstr.indexOf("2e");
////				int cmdcnt  =Integer.parseInt(tmpstr.substring(index+4, index +6));
////				String cmd= tmpstr.substring(index, index + (cmdcnt+4)*2);
////				read_buf =  hexStringToBytes(cmd);
////				String value = CarUtil.toHex(read_buf, (cmdcnt+4));
////				Log.e("jiangyunwei=5", index+"==="+cmd+"==="+value);
////
////				if(read_buf[0]==0x2E){
////					switch(read_buf[1]){
////						case  0x08:
////							if(read_count>=10){
////								Log.e("jiangyunwei=0x09", read_buf[0] + "===" + read_buf[1] + "===" + read_buf[2] + "===" + read_buf[3] + "===" + read_buf[4] + "===" + read_buf[5] + "===" + read_buf[6] + "===" + read_buf[7] + "===" + value);
////
////								second = 0;
////								minute = read_buf[7];
////								hour = read_buf[6];
////								date = read_buf[5];
////								month = read_buf[4];
////								year = Integer.parseInt("20" +read_buf[3]);
////
////								tv_date.setText("20" + read_buf[3] + "-" + (read_buf[4] < 10 ? ("0" + read_buf[4]) : read_buf[4]) + "-" + (read_buf[5] < 10 ? ("0" + read_buf[5]) : read_buf[5]));
////
////							}
////							break;
////
////						case  0x26:
////							if(read_count>=10) {
////								Log.e("jiangyunwei=0x09", read_buf[0] + "===" + read_buf[1] + "===" + read_buf[2] + "===" + read_buf[3] + "===" + read_buf[4] + "===" + value);
////								int degree = read_buf[3];
////								int degree2 = read_buf[4];
////								iv_needle_time.setPivotX(iv_needle_time.getWidth() / 2);
////								iv_needle_time.setPivotY(iv_needle_time.getHeight() / 2);    //支点在图片中心  22:中心偏移量
////
////								if(degree * 16 + degree2 >= 210){
////									iv_needle_time.setRotation(300);  //-120:起始角度
////								}else{
////									iv_needle_time.setRotation(90 + degree * 16 + degree2);  //-120:起始角度
////								}
////
////
////							}
////							break;
////
////						case 0x09:
////							if (read_count >=5) {
////								Log.e("jiangyunwei=开关", rl_time+ "===" +rl_clock+ "===" +read_buf[0] + "===" + read_buf[1] + "===" + read_buf[2] + "===" + read_buf[3] + "===" + value);
////
////								m_myHandler.post(new Runnable() {
////									@Override
////									public void run() {
////										if (read_buf[3] == 1) {
////											rl_time.setVisibility(View.VISIBLE);
////											rl_clock.setVisibility(View.VISIBLE);
////										} else {
////											rl_time.setVisibility(View.GONE);
////											rl_clock.setVisibility(View.GONE);
////										}
////									}
////								});
////
////							}
////							break;
////
////					}
////
////				}
////			}
//		} catch (Exception e) {
//			Log.e("f2===e", e+"===");
//
//			e.printStackTrace();
//		}
//		return 1;
//	}

	
	@Override
	protected void onResume() {
		super.onResume();
	}

	@Override
	protected void onDestroy() {
		super.onDestroy();
	}

	@Override
	public boolean onTouchEvent(MotionEvent event) {
		if (event.getAction() == MotionEvent.ACTION_DOWN) {
			try {
				InputMethodManager imm = (InputMethodManager) getSystemService(INPUT_METHOD_SERVICE);
				return imm.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(), 0);
			} catch (Exception e) {
			}
		}
		return super.onTouchEvent(event);
	}

	public void gotoAct(Class<?> clazz) {
		Intent intent = new Intent(this, clazz);
		startActivity(intent);
	}

	public void finishMine() {
		AppManager.getAppManager().finishActivity(this);
	}
}
